package com.zhitan.netty.server;

import com.zhitan.netty.decoder.ChargingYunDecoder;
import com.zhitan.netty.decoder.ChargingYunEncoder;
import com.zhitan.netty.decoder.MessageDecoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 服务端初始化，客户端与服务器端连接一旦创建，
 * 这个类中方法就会被回调，设置出站编码器和入站解码器
 **/
@Component
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Resource
    private NettyServerHandler nettyServerHandler;

    @Override
    protected void initChannel(SocketChannel channel) {
        channel.pipeline().addLast(new MessageDecoder(Integer.MAX_VALUE, 1, 1, 2, 0));
        channel.pipeline().addLast("decoder", new ChargingYunDecoder());
        channel.pipeline().addLast("encoder", new ChargingYunEncoder());
        channel.pipeline().addLast(nettyServerHandler);
    }
}
